package cst.a931xmc.yuejian20.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import org.w3c.dom.Text;

import cst.a931xmc.yuejian20.Bean.LoginBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.LoginModel;
import cst.a931xmc.yuejian20.R;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText user_tel;
    private EditText user_password;
    private Button btn_login;
    private ImageButton btn_notsee;
    private Button btn_reg;
    private CheckBox ckbx_rmbme;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private LoginModel model;
    private Listener<LoginBean> listener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        initData();
    }

    public void init(){
        user_tel = findViewById(R.id.user_tel);
        user_password = findViewById(R.id.user_password);
        ckbx_rmbme = findViewById(R.id.ckbx_rmbme);
        btn_login = findViewById(R.id.btn_login);
        btn_notsee = findViewById(R.id.btn_notsee);
        btn_reg = findViewById(R.id.btn_reg);
        btn_login.setOnClickListener(this);
        btn_notsee.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
        sp = getSharedPreferences("user_id",MODE_PRIVATE);
        boolean isRemember=sp.getBoolean("remember",false);
        if(isRemember){
            //将账号和密码都设置到文本中
            user_tel.setText(sp.getString("user_name",""));
            user_password.setText(sp.getString("user_password",""));
            ckbx_rmbme.setChecked(true);
        }
    }

    public void initData(){
        listener = new Listener<LoginBean>() {
            @Override
            public void onResponse(LoginBean l) {
                if(l.getUser_id()!=0){
                    editor=sp.edit();
                    if(ckbx_rmbme.isChecked()){
                        editor.putBoolean("remember",true);
                        editor.putString("user_name",user_tel.getText().toString());
                        editor.putString("user_password",user_password.getText().toString());
                    }else{
                        editor.clear();
                    }
                    editor.putInt("user_id", l.getUser_id());
                    editor.putString("user_password",user_password.getText().toString());
                    editor.commit();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(LoginActivity.this,message,Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_login:
                model = new LoginModel();
                model.login(user_tel.getText().toString(), user_password.getText().toString(), listener);
                break;
            case R.id.btn_notsee:
                if(user_password.getInputType()!=InputType.TYPE_CLASS_TEXT) {
                    user_password.setInputType(InputType.TYPE_CLASS_TEXT);
                    btn_notsee.setImageResource(R.drawable.cansee);
                }
                else if(user_password.getInputType()==InputType.TYPE_CLASS_TEXT){
                    user_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    btn_notsee.setImageResource(R.drawable.notsee);
                }
                break;
            case R.id.btn_reg:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
